app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
  /*人员基础管理*/
  .state('employee', {
    url: '/employees',
    templateUrl: 'app/partials/employee/index.html',
    controller: function($scope,$location,$filter,Employee) {
      $scope.widget_title = '员工花名册';
      
        $scope.page_info = {
            filter_value: null,
            perpage_count: 10,
            current_page: 1
        };

         Employee.query(function(result){
          $scope.employees = result
          if(result.length >0){
            $scope.employees_key = _.keys(result[0].roster_attrs)  
          }

          setUpPageData();

        });

        $scope.clear_filter = function(){
          $scope.filter_value = "";
        }

        $scope.pageChanged = function() {
            $scope.perpage= $scope.filter_drivers.
                slice($scope.page_info.perpage_count * ($scope.page_info.current_page - 1),
                      $scope.page_info.perpage_count * $scope.page_info.current_page);
        };

        function setUpPageData() {
            $scope.$watch("filter_value", function(new_value) {
                $scope.filter_drivers = $filter("filter")($scope.employees, new_value);
                $scope.perpage = $scope.filter_drivers.slice(0, $scope.page_info.perpage_count);
            });
        }






     
      
      
      
      $scope.itemEdit = function(item){
        $location.url("/employees/"+item+"/edit")
      }

      /* 删除 记录
      $scope.itemDelete = function(item){
        Employee.delete({},{"id": $scope.employees[item].id})
        $scope.employees.splice(item, 1); 
      }

      

      $scope.edit_certificates = function(item){
        $location.url("/employees/" + item + "/certificates")
      }

      $scope.edit_positions = function(item){
        $location.url("/employees/" + item + "/positions")
      }

      $scope.edit_contracts = function(item){
        $location.url("/employees/" + item + "/contracts")
      }
      */
   }
  })

  /*编辑人员*/
  .state('employee.edit', {
    url: '/:id/edit',
    templateUrl: 'app/partials/employee/form.html',
    controller: function($scope,$state,Employee, $stateParams) {
      $scope.widget_title = '编辑人员';
      $scope.employee = Employee.get({id: $stateParams.id})

      $scope.update = function(employee){
        Employee.update(employee,function(nr){
          $state.transitionTo('employee',$stateParams,{reload: true});
        });
      }
   }
  })

  /*添加人员*/
  .state('employee.new', {
    url: '/new',
    templateUrl: 'app/partials/employee/form.html',
    controller: function($scope,$state,Employee) {
      $scope.widget_title = '添加人员';
      $scope.update = function(employee){
        Employee.save(employee,function(nr){
          $scope.employees.push(nr);
          $scope.employee = {};
        });
        
        
      }
   }
  })
 /*  查看证书*/
  .state('employee.employee_certificate', {
    url: '/:id/certificates',
    templateUrl: 'app/partials/employee/certificate/index.html',
    controller: function($scope,$location,Employee, $stateParams, $state, Certificate, EmployeeCertificate) {
      $scope.employee =  Employee.get({id: $stateParams.id});
      $scope.certificates = Certificate.query();
      $scope.widget_title = '证书编辑';
      /* 删除 记录*/
      $scope.itemDelete = function(item){
        EmployeeCertificate.delete({},{"id": $scope.employee.employee_certificates[item].id})
        $scope.employee.employee_certificates.splice(item, 1); 
      }
      $scope.edit_certificate = function(item){
        $location.url("/employees/"+ $stateParams.id +"/certificates/" + item + "/edit")
      }

     }
  })

  /*  编辑证书*/
  .state('employee.employee_certificate.edit', {
    url: '/:employee_certificate_id/edit',
    templateUrl: 'app/partials/employee/certificate/form.html',
    controller: function($scope,$location,Employee, $stateParams, $state, Certificate, EmployeeCertificate) {
      $scope.widget_title = '证书编辑';
      $scope.employee_certificate = EmployeeCertificate.get({id: $stateParams.employee_certificate_id});
      $scope.update = function(employee_certificate){
        EmployeeCertificate.update(employee_certificate, function(nr){
          $state.transitionTo('employee.employee_certificate',$stateParams,{reload: true});
        }, function(nr){
          
        });
      }
    }
  })

  /*  添加证书*/
  .state('employee.employee_certificate.new', {
    url: '/new',
    templateUrl: 'app/partials/employee/certificate/form.html',
    controller: function($scope,$location, $state, Employee, $stateParams, EmployeeCertificate) {
      $scope.widget_title = '添加证书';

      $scope.update = function(employee_certificate){
        employee_certificate.employee_id = $stateParams.id
        EmployeeCertificate.save(employee_certificate,function(nr){
          $state.transitionTo('employee.employee_certificate',$stateParams,{reload: true});
        });
      }
     }
  })

  /*  查看岗位*/
  .state('employee.employee_position', {
    url: '/:id/positions',
    templateUrl: 'app/partials/employee/positions/index.html',
    controller: function($scope,$location,Employee, $stateParams, $state, Position, EmployeePosition) {
      $scope.employee =  Employee.get({id: $stateParams.id});
      $scope.widget_title = '岗位一览';

      /* 删除 记录*/
      $scope.itemDelete = function(item){
        EmployeePosition.delete({},{"id": $scope.employee.employee_positions[item].id})
        $scope.employee.employee_positions.splice(item, 1); 
      }
      $scope.edit_position = function(item){
        $location.url("/employees/"+ $stateParams.id +"/positions/" + item + "/edit")
      }
     }
  })

  /*  编辑岗位*/
  .state('employee.employee_position.edit', {
    url: '/:employee_position_id/edit',
    templateUrl: 'app/partials/employee/positions/form.html',
    controller: function($scope,$location,Employee, $stateParams, $state, Organization, EmployeePosition) {
      $scope.widget_title = '岗位编辑';
      $scope.organizations = Organization.query();

      EmployeePosition.get({id: $stateParams.employee_position_id}, function(nr){
        $scope.positions = Organization.query_position_standard({id: nr.organization_id}, function(){
          $scope.position = nr
        });
      });
      
      $scope.update = function(position){
        EmployeePosition.update(position, function(nr){
          $state.transitionTo('employee.employee_position',$stateParams,{reload: true});
        });
      }

     }
  })

  /*  添加岗位*/
  .state('employee.employee_position.new', {
    url: '/new',
    templateUrl: 'app/partials/employee/positions/form.html',
    controller: function($scope,$location,Employee, EmployeePosition, $stateParams, Organization) {
      $scope.organizations = Organization.query();
      $scope.widget_title = '分配岗位';
      $scope.update = function(position){
        position.employee_id = $stateParams.id
        EmployeePosition.save(position,function(nr){
          $scope.employee.employee_positions.push(EmployeePosition.get({id: nr.result.id}));
          $state.transitionTo('employee.employee_position',$stateParams,{reload: true});
        });
      }
     }
  })

  /*  编辑合同列表 */
  .state('employee.employee_contract', {
    url: '/:id/contracts',
    templateUrl: 'app/partials/employee/contract/index.html',
    controller: function($scope,$location,Employee, $stateParams, $state, EmployeeContract) {
      $scope.employee =  Employee.get({id: $stateParams.id});
      $scope.widget_title = '合同列表';

      /* 删除 记录*/
      $scope.itemDelete = function(item){
        EmployeeContract.delete({},{"id": $scope.employee.contracts[item].id})
        $scope.employee.contracts.splice(item, 1); 
      }

      $scope.edit_contract = function(item){
        $location.url("/employees/"+ $stateParams.id +"/contracts/" + item + "/edit")
      }
     }
  })

  /*  编辑合同 */
  .state('employee.employee_contract.edit', {
    url: '/:contract_id/edit',
    templateUrl: 'app/partials/employee/contract/form.html',
    controller: function($scope,$location,Employee, $stateParams, $state, ContractType, EmployeeContract) {
      $scope.widget_title = '合同列表';
      $scope.contract_types = ContractType.query();
      $scope.contract = EmployeeContract.get({id: $stateParams.contract_id})

      $scope.update = function(contract){
        EmployeeContract.update(contract, function(nr){
          $state.transitionTo('employee.employee_contract',$stateParams,{reload: true});
        });
      }

     }
  })

  /*  添加合同 */
  .state('employee.employee_contract.new', {
    url: '/new',
    templateUrl: 'app/partials/employee/contract/form.html',
    controller: function($scope,$location,Employee, $stateParams, $state, ContractType, EmployeeContract ) {
      $scope.widget_title = '添加合同';
      $scope.contract_types = ContractType.query();


      $scope.update = function(contract){
        contract.employee_id = $stateParams.id
        EmployeeContract.save(contract, function(nr){
          // $scope.employee.contracts.push(nr.result)
          $state.transitionTo('employee.employee_contract',$stateParams,{reload: true});
        });
      }

     }
  })
})